package com.zzyl.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingPlanServiceImpl implements NursingPlanService {
    final NursingPlanMapper nursingPlanMapper;
    final NursingProjectPlanMapper nursingProjectPlanMapper;
    final NursingLevelMapper nursingLevelMapper;


    /**
     * 根据名称和状态分页查询
     *
     * @param dto 护理计划分页条件查询Dto
     * @return {@code PageBean<NursingPlanVo> }
     * @author YiSheep
     */
    @Override
    public PageBean<NursingPlanVo> pageQuery(NursingPlanPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingPlanVo> page = nursingPlanMapper.pageQuery(dto);
        PageBean<NursingPlanVo> pageBean = PageBean.of(page, NursingPlanVo.class);
        return pageBean;
    }

    /**
     * 添加护理计划
     *
     * @param dto 护理计划Dto
     * @author YiSheep
     */
    @Transactional
    @Override
    public void addNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setCreateBy(1671403256519078138L);
        nursingPlan.setUpdateBy(1671403256519078138L);
        nursingPlan.setStatus(1);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        List<NursingProjectPlanDto> nursingProjectPlanDtos = dto.getProjectPlans();
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(nursingProjectPlanDtos, NursingProjectPlan.class);
        nursingProjectPlans.forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.addNursingProjectPlan(nursingProjectPlan);
        });

    }

    /**
     * 根据ID查询护理计划
     *
     * @param id 护理计划ID
     * @return {@code NursingPlanVo }
     * @author YiSheep
     */
    @Override
    public NursingPlanVo getDetailById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.getByPlanId(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划Dto
     * @author YiSheep
     */
    @Transactional
    @Override
    public void updateNursingPlan(NursingPlanDto dto) {
        if (dto.getStatus() == 1){
            throw new BaseException(BasicEnum.UPDATE_PLAN_STATUS_FAIL);
        }
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1671403256519078138L);
        nursingPlanMapper.updateById(nursingPlan);

        nursingProjectPlanMapper.deleteByPlanId(dto.getId());
        List<NursingProjectPlanDto> nursingProjectPlanDtos = dto.getProjectPlans();
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(nursingProjectPlanDtos, NursingProjectPlan.class);
        nursingProjectPlans.forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(dto.getId());
            nursingProjectPlanMapper.addNursingProjectPlan(nursingProjectPlan);
        });
    }

    /**
     * 删除护理计划
     *
     * @param id 护理计划ID
     * @author YiSheep
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        //判断护理计划的状态是否启用，如果启用，抛出异常
        if (nursingPlan.getStatus() == 1){
            throw new BaseException(BasicEnum.DELETE_PLAN_STATUS_FAIL);
        }
        //判断该护理计划是否关联护理等级
        List<NursingPlan> nursingPlans = nursingLevelMapper.getByPlanId(id);
        if (CollUtil.isNotEmpty(nursingPlans)){
            throw new BaseException(BasicEnum.DELETE_PLAN_REF_FAIL);
        }
        nursingProjectPlanMapper.deleteByPlanId(id);
        nursingPlanMapper.deleteById(id);
    }

    /**
     * 启用/禁用护理计划
     *
     * @param id     护理计划ID
     * @param status 状态
     * @author YiSheep
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingPlan nursingPlan = NursingPlan.builder().id(id).status(status).build();
        nursingPlanMapper.updateById(nursingPlan);
    }

    /**
     * 查询所有护理计划
     *
     * @return {@code List<NursingPlan> }
     * @author YiSheep
     */
    @Override
    public List<NursingPlan> getAllNursingPlan() {
        return nursingPlanMapper.getAllNursingPlan();
    }
}
